define([
    'app',
    'js/api/OrderAPI',
    'js/api/ProductAPI'
    ], function (app) {
    'use strict';
    app.controller('casioCtrl', ['$scope','$rootScope', '$http', '$stateParams', '$location', 'OrderAPI', 'ProductAPI', function($scope, $rootScope, $http, $stateParams, $location, OrderAPI, ProductAPI) {

        
        $scope.content = true;
        $scope.size = false;
        $scope.moveToContent = function () {
            $scope.content = true;
            $scope.size = false;
            mySwiper1.slideTo(0);
        }

        $scope.moveToSize = function () {
            $scope.content = false;
            $scope.size = true;
            mySwiper1.slideTo(1);
        }

        var mySwiper = new Swiper('.content-info', {

        });

        var mySwiper1 = new Swiper('.product-tab', {
            noSwipingClass: 'swiper-no-swiping',
        });


        function getProductdata() {
            var name = $stateParams.name;
            console.log(name);  
            ProductAPI.dataUrl(name).then(function(data) {
                console.log(data);   
                if(data.model.objData.panels.size) {
                    $scope.needRingSize = true;   
                    $scope.ringSizeData = data.model.objData.panels.size.options.ringSize.data;

                }  
                var cdn = 'http://cdn.m-lab.cn/';
                for (var i in data.images) {
                    if (data.images[i]['main']) {
                        $scope.mainImg = cdn + data.images[i]['main'];
                        break;
                    }
                }
                function initOrderObj(options, thirdparty){
                    var getPrice = eval(data.priceCount.js);
                    var price = getPrice(options) * 100;
                    var orderObj = {
                        platform: {
                            name: thirdparty
                        },
                        subject: '马良行-' + thirdparty,
                        body: '陌生来信',
                        allSubTotal: price,
                        allQuantity: 1,
                        allDiscountAmount: 0,
                        allShippingCost: 0,
                        allTotal: price,
                        change: 0,
                        item: [],
                        message: $scope.phone.input || thirdpartyOrderId,
                        note: thirdparty + '订单',
                        address: {
                            name: $scope.address.name,
                            phone: $scope.address.phone,
                            province: {
                                objectId: '26',
                                __type: 'Pointer',
                                className: 'Address_province'
                            },
                            city: {
                                objectId: '382',
                                __type: 'Pointer',
                                className: 'Address_city'
                            },
                            detail: $scope.address.address
                        }
                    };

                    orderObj.item.push({
                        product: {
                            __type: 'Pointer',
                            className: 'Product',
                            objectId: data.objectId
                        },
                        options: options,
                        price: price,
                        priceDiscount: price,
                        subTotal: price,
                        total: price,
                        quantity: 1,
                        shippingCost: 0
                    });
                    return orderObj;
                }
                $scope.confirm = function(){
                    if($scope.submitting) return;
                    if(!$scope.wavUrl) {
                        alert('请先录制语音');
                        return;
                    }
                    if($scope.needRingSize && !$scope.ringSize){
                        alert('请选择戒围');
                        return;
                    }
                    $scope.submitting = true;
                    $scope.confirmBtn = '提交中';
                    var orderObj = initOrderObj({
                        className : data.model.objData.object,
                        materialName: 'silver',
                        heights: $scope.heights,
                        wavUrl: $scope.wavUrl,
                        ringSize: $scope.ringSize,
                        type: $scope.type
                    }, thirdparty);


                    OrderAPI.anonymousOrder(orderObj).then(function(data) {
                        
                        
                        alert('录音成功，无需重复提交');
                        if(redirectUri) {
                            $scope.confirmBtn = '下单成功';
                            location.href = redirectUri.slice(0, 4) == 'http' ? redirectUri : 'http://' + redirectUri;
                        } else {
                            location.reload();
                        }

                    }, function(error) {
                        console.log('下单失败:', error);
                        alert('录音失败，请重新录制')
                        $scope.submitting = false;
                        $scope.confirmBtn = '确定';
                    });
                }



            });
        }
        getProductdata();


        var thirdpartyName = {
            'yangqi' : '氧气',
            'xmly' : '喜马拉雅',
            'liangcang' : '良仓',
            'hofo' : 'hofo',
            'touch': '触宝',
            'jianjiao': '尖叫'
        };
        var thirdpartyOrderId = $location.search().order_id || 'null';
        var redirectUri = $location.search().redirect_uri || 'm.malianghang.com';
        var thirdpartyId = $location.search().thirdparty_id || $location.search().from || 'liangcang';
        var thirdparty = '卡西欧';
        $scope.type = $location.search().type;
        function validateuser(mobile) {
           if(!validateSerial(mobile)) 
           { 
               alert('对不起，您不是受邀用户！'); 
               return false; 
           } 
           return true; 
        }
        function validatemobile(mobile) {
           if(mobile.length==0) 
           { 
              alert('请输入手机号码！'); 
              return false; 
           }     
           if(mobile.length!=11) 
           { 
               alert('请输入有效的手机号码！'); 
               return false; 
           } 
            
           var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/; 
           if(!myreg.test(mobile)) 
           { 
               alert('请输入有效的手机号码！'); 
               return false; 
           }  
           return true;
        } 

        function closePhoneDialog(){
            var step1 = document.getElementById('step1');
            var step3 = document.getElementById('step3');
            step1.style.opacity = 1;
            step3.style.display = 'none';
        }
        $scope.showCarve = true;
        // if(thirdpartyId == 'liangcang' || thirdpartyId == 'yangqi') {
        //     $scope.showCarve = false;
        //     closePhoneDialog();
        // }
        $scope.phone = {};
        $scope.address = {
            phone: '',
            name: '',
            address: ''
        };
        $scope.phone.input = '';
        $scope.getPhone = function () {
            if(validateuser($scope.phone.input)) {
                // $scope.address.phone = $scope.phone.input;
                // closePhoneDialog();
                $scope.state = 'address'
            }
                //closePhoneDialog();
        }
        $scope.getAddress = function () {
            if($scope.address.address.length < 5) {
                alert('请输入有效地址')
                return;
            }
            if($scope.address.name.length < 1) {
                alert('请输入有效收货人')
                return;
            }
            if(!validatemobile($scope.address.phone)) {
                return;
            }
            $scope.state = 'introduction'
            var step1 = document.getElementById('step1');
            step1.style.opacity = 1;
        }


        $scope.state = 'check'


        


        $scope.submitting = false;
        $scope.confirmBtn = '确定';
        


        function drawWaveform(waveHeights, options) {
            var x = options.x;
            var y = options.y;
            var w = options.w;
            var h = options.h;
            var maxHeight = -1000;
            for (var i = 0; i < waveHeights.length; i++) {
                maxHeight = maxHeight > waveHeights[i] ? maxHeight : waveHeights[i];
            }
            var canvas = document.getElementById("carve");
            if (canvas) {
                var ctx = canvas.getContext("2d");
                ctx.lineWidth = 2;
                ctx.strokeStyle = 'rgba(180,180,180,1)';
                ctx.shadowOffsetX = 0.7; // 阴影Y轴偏移
                ctx.shadowOffsetY = 0.7; // 阴影X轴偏移
                ctx.shadowBlur = 0.5; // 模糊尺寸
                ctx.shadowColor = 'rgba(255, 255, 255, 1)'; // 颜色


                ctx.clearRect(0, 0, 1000, 1000);
                ctx.beginPath();
                ctx.moveTo(x, y);
                var count = waveHeights.length;
                var scale = h / (maxHeight);
                var step = w / (count + 1);
                for (var i = 0; i < waveHeights.length; i++) {
                    ctx.lineTo((i) * step + x, y + scale * (waveHeights[i]));

                }
                ctx.stroke();


            }
        }

        if($rootScope.waveformEvent1) $rootScope.waveformEvent1();
        $rootScope.waveformEvent1 = $rootScope.$on('waveform', function(event, waveHeights, data, wavUrl, localId, warning){
            $scope.heights = data;
            $scope.wavUrl = wavUrl;
            drawWaveform(waveHeights, {x: 0, y: 40, w: 240, h: 40*0.3});

            
        });



        function validateSerial(serial){
            var list = [ '52217761F',
                '52217762F',
                '52217763F',
                '52217764F',
                '52217765F',
                '52217766F',
                '52217767F',
                '52217768F',
                '52217769F',
                '52217770F',
                '52217791F',
                '52217792F',
                '52217793F',
                '52217794F',
                '52217795F',
                '52217796F',
                '52217797F',
                '52217798F',
                '52217799F',
                '52217800F',
                '52217821F',
                '52217822F',
                '52217823F',
                '52217824F',
                '52217825F',
                '52217826F',
                '52217827F',
                '52217828F',
                '52217829F',
                '52217830F',
                '52217841F',
                '52217842F',
                '52217843F',
                '52217844F',
                '52217845F',
                '52217846F',
                '52217847F',
                '52217848F',
                '52217849F',
                '52217850F',
                '52217851F',
                '52217852F',
                '52217853F',
                '52217854F',
                '52217855F',
                '52217856F',
                '52217857F',
                '52217858F',
                '52217859F',
                '52217860F',
                '52217891F',
                '52217892F',
                '52217893F',
                '52217894F',
                '52217895F',
                '52217896F',
                '52217897F',
                '52217898F',
                '52217899F',
                '52217900F',
                '52217911F',
                '52217912F',
                '52217913F',
                '52217914F',
                '52217915F',
                '52217916F',
                '52217917F',
                '52217918F',
                '52217919F',
                '52217920F',
                '52217921F',
                '52217922F',
                '52217923F',
                '52217924F',
                '52217925F',
                '52217926F',
                '52217927F',
                '52217928F',
                '52217929F',
                '52217930F',
                '52217931F',
                '52217932F',
                '52217933F',
                '52217934F',
                '52217935F',
                '52217936F',
                '52217937F',
                '52217938F',
                '52217939F',
                '52217940F',
                '52217941F',
                '52217942F',
                '52217943F',
                '52217944F',
                '52217945F',
                '52217946F',
                '52217947F',
                '52217948F',
                '52217949F',
                '52217950F',
                '52217961F',
                '52217962F',
                '52217963F',
                '52217964F',
                '52217965F',
                '52217966F',
                '52217967F',
                '52217968F',
                '52217969F',
                '52217970F',
                '52217991F',
                '52217992F',
                '52217993F',
                '52217994F',
                '52217995F',
                '52217996F',
                '52217997F',
                '52217998F',
                '52217999F',
                '52218000F',
                '52218011F',
                '52218012F',
                '52218013F',
                '52218014F',
                '52218015F',
                '52218016F',
                '52218017F',
                '52218018F',
                '52218019F',
                '52218020F',
                '52218021F',
                '52218022F',
                '52218023F',
                '52218024F',
                '52218025F',
                '52218026F',
                '52218027F',
                '52218028F',
                '52218029F',
                '52218030F',
                '52218031F',
                '52218032F',
                '52218033F',
                '52218034F',
                '52218035F',
                '52218036F',
                '52218037F',
                '52218038F',
                '52218039F',
                '52218040F',
                '52218071F',
                '52218072F',
                '52218073F',
                '52218074F',
                '52218075F',
                '52218076F',
                '52218077F',
                '52218078F',
                '52218079F',
                '52218080F',
                '52218081F',
                '52218082F',
                '52218083F',
                '52218084F',
                '52218085F',
                '52218086F',
                '52218087F',
                '52218088F',
                '52218089F',
                '52218090F',
                '52218121F',
                '52218122F',
                '52218123F',
                '52218124F',
                '52218125F',
                '52218126F',
                '52218127F',
                '52218128F',
                '52218129F',
                '52218130F',
                '52218131F',
                '52218132F',
                '52218133F',
                '52218134F',
                '52218135F',
                '52218136F',
                '52218137F',
                '52218138F',
                '52218139F',
                '52218140F',
                '52218151F',
                '52218152F',
                '52218153F',
                '52218154F',
                '52218155F',
                '52218156F',
                '52218157F',
                '52218158F',
                '52218159F',
                '52218160F',
                '52218161F',
                '52218162F',
                '52218163F',
                '52218164F',
                '52218165F',
                '52218166F',
                '52218167F',
                '52218168F',
                '52218169F',
                '52218170F',
                '52218171F',
                '52218172F',
                '52218173F',
                '52218174F',
                '52218175F',
                '52218176F',
                '52218177F',
                '52218178F',
                '52218179F',
                '52218180F',
                '52218181F',
                '52218182F',
                '52218183F',
                '52218184F',
                '52218185F',
                '52218186F',
                '52218187F',
                '52218188F',
                '52218189F',
                '52218190F',
                '52218191F',
                '52218192F',
                '52218193F',
                '52218194F',
                '52218195F',
                '52218196F',
                '52218197F',
                '52218198F',
                '52218199F',
                '52218200F',
                '52218201F',
                '52218202F',
                '52218203F',
                '52218204F',
                '52218205F',
                '52218206F',
                '52218207F',
                '52218208F',
                '52218209F',
                '52218210F',
                '52218211F',
                '52218212F',
                '52218213F',
                '52218214F',
                '52218215F',
                '52218216F',
                '52218217F',
                '52218218F',
                '52218219F',
                '52218220F',
                '52218221F',
                '52218222F',
                '52218223F',
                '52218224F',
                '52218225F',
                '52218226F',
                '52218227F',
                '52218228F',
                '52218229F',
                '52218230F',
                '52218231F',
                '52218232F',
                '52218233F',
                '52218234F',
                '52218235F',
                '52218236F',
                '52218237F',
                '52218238F',
                '52218239F',
                '52218240F',
                '52218301F',
                '52218302F',
                '52218303F',
                '52218304F',
                '52218305F',
                '52218306F',
                '52218307F',
                '52218308F',
                '52218309F',
                '52218310F',
                '52218311F',
                '52218312F',
                '52218313F',
                '52218314F',
                '52218315F',
                '52218316F',
                '52218317F',
                '52218318F',
                '52218319F',
                '52218320F',
                '52218322F',
                '52218325F',
                '52218328F',
                '52218331F',
                '52218332F',
                '52218333F',
                '52218334F',
                '52218335F',
                '52218336F',
                '52218337F',
                '52218338F',
                '52218339F',
                '52218340F',
                '52218341F',
                '52218342F',
                '52218343F',
                '52218344F',
                '52218345F',
                '52218346F',
                '52218347F',
                '52218348F',
                '52218349F',
                '52218350F',
                '52218351F',
                '52218352F',
                '52218353F',
                '52218354F',
                '52218355F',
                '52218356F',
                '52218357F',
                '52218358F',
                '52218359F',
                '52218360F',
                '57401461F',
                '57401462F',
                '57401463F',
                '57401464F',
                '57401465F',
                '57401466F',
                '57401467F',
                '57401468F',
                '57401469F',
                '57401470F',
                '57401551F',
                '57401552F',
                '57401553F',
                '57401554F',
                '57401555F',
                '57401556F',
                '57401557F',
                '57401558F',
                '57401559F',
                '57401560F',
                '57401601F',
                '57401602F',
                '57401603F',
                '57401604F',
                '57401605F',
                '57401606F',
                '57401607F',
                '57401608F',
                '57401609F',
                '57401610F',
                '57401631F',
                '57401632F',
                '57401633F',
                '57401634F',
                '57401635F',
                '57401636F',
                '57401637F',
                '57401638F',
                '57401639F',
                '57401640F',
                '57401641F',
                '57401642F',
                '57401643F',
                '57401644F',
                '57401645F',
                '57401646F',
                '57401647F',
                '57401648F',
                '57401649F',
                '57401650F',
                '57401651F',
                '57401652F',
                '57401653F',
                '57401654F',
                '57401655F',
                '57401656F',
                '57401657F',
                '57401658F',
                '57401659F',
                '57401660F',
                '57401681F',
                '57401682F',
                '57401683F',
                '57401684F',
                '57401685F',
                '57401686F',
                '57401687F',
                '57401688F',
                '57401689F',
                '57401690F',
                '57401691F',
                '57401692F',
                '57401693F',
                '57401694F',
                '57401695F',
                '57401696F',
                '57401697F',
                '57401698F',
                '57401699F',
                '57401700F',
                '57401701F',
                '57401702F',
                '57401703F',
                '57401704F',
                '57401705F',
                '57401706F',
                '57401707F',
                '57401708F',
                '57401709F',
                '57401710F',
                '57401711F',
                '57401712F',
                '57401713F',
                '57401714F',
                '57401715F',
                '57401716F',
                '57401717F',
                '57401718F',
                '57401719F',
                '57401720F',
                '57401731F',
                '57401732F',
                '57401733F',
                '57401734F',
                '57401735F',
                '57401736F',
                '57401737F',
                '57401738F',
                '57401739F',
                '57401740F',
                '57401781F',
                '57401782F',
                '57401783F',
                '57401784F',
                '57401785F',
                '57401786F',
                '57401787F',
                '57401788F',
                '57401789F',
                '57401790F',
                '57401841F',
                '57401842F',
                '57401843F',
                '57401844F',
                '57401845F',
                '57401846F',
                '57401847F',
                '57401848F',
                '57401849F',
                '57401850F',
                '57401851F',
                '57401852F',
                '57401853F',
                '57401854F',
                '57401855F',
                '57401856F',
                '57401857F',
                '57401858F',
                '57401859F',
                '57401860F',
                '57401901F',
                '57401902F',
                '57401903F',
                '57401904F',
                '57401905F',
                '57401906F',
                '57401907F',
                '57401908F',
                '57401909F',
                '57401910F',
                '57402001F',
                '57402002F',
                '57402003F',
                '57402004F',
                '57402005F',
                '57402006F',
                '57402007F',
                '57402008F',
                '57402009F',
                '57402010F',
                '57402041F',
                '57402042F',
                '57402043F',
                '57402044F',
                '57402045F',
                '57402046F',
                '57402047F',
                '57402048F',
                '57402049F',
                '57402050F',
                '57402081F',
                '57402082F',
                '57402083F',
                '57402084F',
                '57402085F',
                '57402086F',
                '57402087F',
                '57402088F',
                '57402089F',
                '57402090F',
                '57402111F',
                '57402112F',
                '57402113F',
                '57402114F',
                '57402115F',
                '57402116F',
                '57402117F',
                '57402118F',
                '57402119F',
                '57402120F',
                '57402211F',
                '57402212F',
                '57402213F',
                '57402214F',
                '57402215F',
                '57402216F',
                '57402217F',
                '57402218F',
                '57402219F',
                '57402220F',
                '57402231F',
                '57402232F',
                '57402233F',
                '57402234F',
                '57402235F',
                '57402236F',
                '57402237F',
                '57402238F',
                '57402239F',
                '57402240F',
                '57402271F',
                '57402272F',
                '57402273F',
                '57402274F',
                '57402275F',
                '57402276F',
                '57402277F',
                '57402278F',
                '57402279F',
                '57402280F',
                '57402291F',
                '57402292F',
                '57402293F',
                '57402294F',
                '57402295F',
                '57402296F',
                '57402297F',
                '57402298F',
                '57402299F',
                '57402300F',
                '57402311F',
                '57402312F',
                '57402313F',
                '57402314F',
                '57402315F',
                '57402316F',
                '57402317F',
                '57402318F',
                '57402319F',
                '57402320F',
                '57402331F',
                '57402332F',
                '57402333F',
                '57402334F',
                '57402335F',
                '57402336F',
                '57402337F',
                '57402338F',
                '57402339F',
                '57402340F',
                '57402341F',
                '57402342F',
                '57402343F',
                '57402344F',
                '57402345F',
                '57402346F',
                '57402347F',
                '57402348F',
                '57402349F',
                '57402350F',
                '57402371F',
                '57402372F',
                '57402373F',
                '57402374F',
                '57402375F',
                '57402376F',
                '57402377F',
                '57402378F',
                '57402379F',
                '57402380F',
                '57402391F',
                '57402392F',
                '57402393F',
                '57402394F',
                '57402395F',
                '57402396F',
                '57402397F',
                '57402398F',
                '57402399F',
                '57402400F',
                '57402411F',
                '57402412F',
                '57402413F',
                '57402414F',
                '57402415F',
                '57402416F',
                '57402417F',
                '57402418F',
                '57402419F',
                '57402420F',
                '57402431F',
                '57402432F',
                '57402433F',
                '57402434F',
                '57402435F',
                '57402436F',
                '57402437F',
                '57402438F',
                '57402439F',
                '57402440F',
                '57402441F',
                '57402442F',
                '57402443F',
                '57402444F',
                '57402445F',
                '57402446F',
                '57402447F',
                '57402448F',
                '57402449F',
                '57402450F',
                '57402471F',
                '57402472F',
                '57402473F',
                '57402474F',
                '57402475F',
                '57402476F',
                '57402477F',
                '57402478F',
                '57402479F',
                '57402480F',
                '57402481F',
                '57402483F',
                '57402485F',
                '57402491F',
                '57402492F',
                '57402493F',
                '57402494F',
                '57402495F',
                '57402496F',
                '57402497F',
                '57402498F',
                '57402499F',
                '57402500F' ];
            return list.indexOf(serial) !== -1
        }




    }])
});
